/**
 * Created by Administrator on 2016/7/8.
 */
// 价钱数据处理
closeDiv();
var pageNumber = 1;
var dwpanduan = false;
var dingweiid;
// 分页数据
var mingcheng='';
var danjia=null;
var jinyongbz=0;
// 将类型LI添加到类型UL
addJianChaxlx();
/** 
 * @Title: getJianChax 
 * @Description: 列表页面查询加载数据
 */
function getJianChax(){
	var yibaoxmbm = '';
    var params = {
        "mingcheng": mingcheng,
        "jinyongbz": jinyongbz,
        "yibaoxmbm": yibaoxmbm,
        "rukou":"1"
   		};
      $.ajax({
        type: "get",
        url: "./jianchaxdy/getCount",
        data: params,
    	async: false,
        success: function (data) {
    	totalNumber = data;
    	}
    });
    
    function createJiancx(name) {
    	$('#tbjianchax').html('');
		var container = $('#pagination-'+name);
		container.pagination({
		dataSource:'./jianchaxdy/getByPage?mingcheng='+encodeURI(mingcheng)+'&jinyongbz='+encodeURI(jinyongbz)
		+'&yibaoxmbm='+yibaoxmbm+'&rukou=1',
		locator: '',
		pageSize: 12,
		totalNumber: totalNumber,
		showNavigator: true,
		pageNumber: pageNumber,
   	    formatNavigator: '共 <%= totalNumber %> 条',
		callback: function(data, pagination){
			$('#tbjianchax').html('');
    		var content = '';
        	$.each(data,function(i,v){
				var perPageRecordNum= pagination.pageSize;//每页数据行数
		        var pageNo = pagination.pageNumber;//当前页号
		        var index=((pageNo-1)*perPageRecordNum)+i+1;
      			content = "<tr id='jcx"+this.id+"'>"
						+"<td width='5%'><label class='col-sm-12' style='margin-bottom:0;'>"+index+"</label></td>"
						+"<td width='15%' style='overflow: visible;position: relative;'>"
		                +"<input type='text' id='lb-leixing-in' class='zhuangtai u-input dropdown-toggle input-sm col-sm-12 xiangmufl' value='1' id='lb-leixing-div' placeholder='请输入项目名称' style='border-radius: 0px;width: 100%;'>"
		                +"<ul class='dropdown-menu jiachaxlb'  role='menu' id='lb-leixing-ul' style='border:none;left:3px;top:36px;width:98%;'></ul>"
						+"</td>"
						+"<td width='40%' style='overflow: visible;position: relative;'>"
						+"<input type='text' class='input-sm col-sm-12 u-input dropdown-toggle xiangmumcjy jianchaxmc' id='xiangmumc' name='dian-inp' placeholder='请输入项目名称' style='border-radius: 0px;width: 100%;' />" 
						+'<ul class="dropdown-menu jianchax" style="display: none;border:none;left:3px;top:34px;width:98%;padding:0px;" role="menu"></ul>' 
						+"</td>"
						+"<td width='15%'><input type='text' class='input-sm col-sm-12 u-input inp-select xiangmujgjy' id='xiangmujg' name='dian-inp' style='text-align:right' placeholder='请输入价格'/></td>"
						+"<td width='15%'  style='overflow: visible;'>"
						+"<div class='menu-up choose dropdown-search-li none-border' style='position: relative;'>"
		                +"<input type='hidden' id='lb-jinyongbz-in' class='zhuangtai' value='0'>"
		                +"<div  class='btn dropdown-toggle select-menu' data-toggle='dropdown' tabindex='0' id='lb-jinyongbz-div'>"
		                +"<span>启用<b></b></span></div><ul class= 'dropdown-menu'  role='menu' id='lb-jinyongbz-ul'>"
		                +"<li value='0'>启用</li><li value='1'>禁用</li></ul></div>"      
						+"<td width='15%'>"    
						+'<a class="operation-left"  onclick="jyxm_tianjia('+data[i].id+','+data[i].id+')">保存</a>'
						+'</td></tr>';
      			$("#tbjianchax").append(content);
				var jg = getFloatStr(data[i].danjia);
				$("#jcx"+this.id).find("#xiangmumc").val(data[i].mingcheng);
				$("#jcx"+this.id).find("#xiangmujg").val(jg);
				// 禁启数据读取
				$("#jcx"+this.id).find("#lb-jinyongbz-in").val(data[i].jinyongbz);
				if(data[i].jinyongbz == 0){
					var text='启用';
				}else{
					var text='禁用';
				}
				$("#jcx"+this.id).find("#lb-jinyongbz-div").text(text);
  	            $("#jcx"+this.id).find("#lb-leixing-in").val(this.leixing);
			});
        		addJianChaxlx();
        	  // 定位判断
        	  if(dwpanduan) {
	        	  $('#tbjianchax').find('tr[id="jcx'+dingweiid+'"]').addClass('list-tr');
	        	  $('#tbjianchax').find('tr[id="jcx'+dingweiid+'"] input').addClass('list-tr');
        	  }
		}
	});
	return container;
   }
    createJiancx('fenyjcx');
	if(totalNumber <= 12) {
		$('#pagination-fenyjcx').css('display','none');
	}else{
		$('#pagination-fenyjcx').css('display','block');
	}
}
//select模拟
$("#lbt-jinyongbz-ul li").click(function(){
	var bText = $(this).text();
	var bValue = $(this).val();
	$(this).parent().parent().find(".dropdown-toggle").text("");
	var $span = $("<span></span>");
	$(this).parent().parent().find('.dropdown-toggle').append($span);
	$(this).parent().parent().find('.dropdown-toggle span').text(bText);
	$(this).parent().parent().find("#lbt-jinyongbz-in").val(bValue);
	$(this).parent().css("display","none");
 });

//select模拟
$("#cx-jinyongbz-ul li").click(function(){
	pageNumber = 1;
	var bText = $(this).text();
	var bValue = $(this).val();
	$(this).parent().parent().find(".dropdown-toggle").text("");
	var $span = $("<span></span>");
	$(this).parent().parent().find('.dropdown-toggle').append($span);
	$(this).parent().parent().find('.dropdown-toggle span').text(bText);
	$(this).parent().parent().find("#cx-jinyongbz-in").val(bValue);
	$(this).parent().css("display","none");
// 下拉框立即执行查询
    mingcheng= $("#f-le-xingmumc").val();
    jinyongbz =  $("#cx-jinyongbz-in").val();
// 显示全部信息判断
    if(jinyongbz==2){
    	jinyongbz='';
    }
     // 类型判断
    pageNumber = 1;
	dwpanduan = false;
    getJianChax();
 });

/*加载下拉菜单*/
$(document).on('click','#lb-jinyongbz-ul li',function(){
	var bText = $(this).text();
	var bValue = $(this).val();
	$(this).parent().parent().find(".dropdown-toggle").text("");
	var $span = $("<span></span>");
	$(this).parent().parent().find('.dropdown-toggle').append($span);
	$(this).parent().parent().find('.dropdown-toggle span').text(bText);
	$(this).parent().parent().find("#lb-jinyongbz-in").val(bValue);
	$(this).parent().css("display","none");
});
// 查询按钮
$("#jyxm-search").click(function(){
     mingcheng= $("#f-le-xingmumc").val();
     jinyongbz = ($("#cx-jinyongbz-in").val() == 2) ? '': $("#cx-jinyongbz-in").val();
     // 类型判断
     pageNumber = 1;
	 dwpanduan = false;
     getJianChax();
     return false;
});

// 查询条件清空按钮
$("#jyxm-reset").click(function () {
	$("#f-le-xingmumc").val("");
    $('#cx-jinyongbz-in').val(0)
    $('#cx-jinyongbz-div').text("");
	var $span = $("<span></span>");
	$('#cx-jinyongbz-div').append($span);
	$('#cx-jinyongbz-div span').text("启用");
    mingcheng= $("#f-le-xingmumc").val();
	jinyongbz = ($("#cx-jinyongbz-in").val() == 2) ? '': $("#cx-jinyongbz-in").val();
    // 类型判断
    pageNumber = 1;
	dwpanduan = false;
    getJianChax();
    return false;
});
  
// 添加按钮
$("#jy-add").click(function(){
	if($("#lbt-leixing-in").val() == "" ){
		notifyWarning("诊疗项分类不能为空");
		$("#lbt-leixing-in").addClass("f-jiaoyan");
		return false;
	}else{
		$("#lbt-leixing-in").removeClass("f-jiaoyan");
	}
	if($("#jcxf-txiangmumc").val() == "" ){
		notifyWarning("诊疗项名称不能为空");
		$("#jcxf-txiangmumc").addClass("f-jiaoyan");
		return false;
	}else{
		$("#jcxf-txiangmumc").removeClass("f-jiaoyan");
	}
	if($("#jcxf-txiangmujg").val() == "" ){
		notifyWarning("诊疗项价格不能为空");
		$("#jcxf-txiangmujg").addClass("f-jiaoyan");
		return false;
	}else{
		$("#jcxf-txiangmujg").removeClass("f-jiaoyan");
	}
	if($("#lbt-leixing-in").val().length>100){
		$("#lbt-leixing-in").addClass("f-jiaoyan");
		notifyWarning("诊疗项分类长度应小于100");
		return false;
	}
		if($("#jcxf-txiangmumc").val().length>128){
			$("#jcxf-txiangmumc").addClass("f-jiaoyan");
			notifyWarning("诊疗项名称长度应小于128");
			return false;
		}
		if($("#jcxf-txiangmujg").val().length>10){
			$("#jcxf-txiangmujg").addClass("f-jiaoyan");
			notifyWarning("诊疗项价格长度应小于10");
			return false;
		}
		$('#lb-leixing-ul').html('');
		var add_mingcheng = $("#jcxf-txiangmumc").val();
		var add_leixing =$("#lbt-leixing-in").val();
		var add_danjia =$("#jcxf-txiangmujg").val();
		var add_jinyongbz =$("#lbt-jinyongbz-in").val();
		
		var sendData = {
		"mingcheng":add_mingcheng,
		"leixing":add_leixing,
		"danjia":add_danjia,
		"jinyongbz":add_jinyongbz
		};
		$.ajax({
			type:"POST",
			url:'./jianchaxdy/xinjianjcx',
			data:sendData,
			success:function(data){
		    	//诊所重复检查项
				if (data == MSG_CHONGFUBZ) {
		        	notifyWarning("该诊疗项已在诊所中");
		        	return false;
				}
				$("#jcxf-txiangmumc").val("");
				$("#jcxf-txiangmujg").val("");
				$("#lbt-leixing-in").val('');
				$("#lb-jinyongbz-in").val(0);
				notifySuccess('保存成功');
				getJianChax();
			}  
		}); 
});
var row_id = 0;
function jyxm_quxiao(id){
	row_id=id;
    $("#delete").modal(); 	
}

//信息重置按钮
$("#jy-shanchu").click(function(){
    $("#jcxf-txiangmumc").val("");
    $("#jcxf-txiangmujg").val("");
    $('#lbt-leixing-in').val('');
	$('#lbt-jinyongbz-in').val(0);
	$('#lbt-jinyongbz-div').text("");
	var $span = $("<span></span>");
	$('#lbt-jinyongbz-div').append($span);
	$('#lbt-jinyongbz-div span').text("启用");
	$("#jcxf-txiangmumc").removeClass("f-jiaoyan");
	$("#jcxf-txiangmujg").removeClass("f-jiaoyan");
});


 //列表数据保存
function jyxm_tianjia(id,tid){
	$(".u-input").removeClass("td-border1");
	if($("#jcx"+tid).find("#lb-leixing-in").val()==''){
		 $("#jcx"+tid).find("#lb-leixing-in").addClass("f-jiaoyan");
		 notifyWarning("诊疗项分类不能为空");
		 return false;
	}else{
	 $("#jcx"+tid).find("#lb-leixing-in").removeClass("f-jiaoyan");	
	}
	if($("#jcx"+tid).find("#xiangmumc").val()==''){
		 $("#jcx"+tid).find("#xiangmumc").addClass("f-jiaoyan");
		 notifyWarning("诊疗项名称不能为空");
		 return false;
	}else{
	 $("#jcx"+tid).find("#xiangmumc").removeClass("f-jiaoyan");	
	}
	if($("#jcx"+tid).find("#lb-leixing-in").val().length>100){
		$("#jcx"+tid).find("#lb-leixing-in").addClass("f-jiaoyan");
		notifyWarning("诊疗项名称长度应小于100");
		return false;
	}else{
	 $("#jcx"+tid).find("#lb-leixing-in").removeClass("f-jiaoyan");	
	}
	if($("#jcx"+tid).find("#xiangmumc").val().length>128){
		$("#jcx"+tid).find("#xiangmumc").addClass("f-jiaoyan");
		notifyWarning("诊疗项名称长度应小于128");
		return false;
	}else{
	 $("#jcx"+tid).find("#xiangmumc").removeClass("f-jiaoyan");	
	}
	if($("#jcx"+tid).find("#xiangmujg").val().length>10){
		 $("#jcx"+tid).find("#xiangmujg").addClass("f-jiaoyan");
		 notifyWarning("诊疗项价格长度应小于10");
		 return false;
	}else{
	 $("#jcx"+tid).find("#xiangmumc").removeClass("f-jiaoyan");	
	}
	var trid = tid;
    var add_id = id;
    var add_mingcheng =  $("#jcx"+tid).find("#xiangmumc").val();
    var add_leixing = $("#jcx"+tid).find("#lb-leixing-in").val();
    var add_danjia = $("#jcx"+tid).find("#xiangmujg").val();
    var jinyongbz =  $("#jcx"+tid).find("#lb-jinyongbz-in").val();
    var sendData = {
      "id":add_id,
      "mingcheng":add_mingcheng,
      "leixing":add_leixing,
      "danjia":add_danjia,
      "jinyongbz":jinyongbz
	 };
	$.ajax({
    	type:"POST",
    	url:'./jianchaxdy/xiugaijcx',
    	data:sendData,
    	success:function(data){
		    //诊所重复检查项
			if (data == MSG_CHONGFUBZ) {
	        	notifyWarning("该诊疗项已在诊所中");
	        	return false;
			}
    	 	notifySuccess('保存成功');
	    	getJianChax();
    	}
  });      
}
//检查项添加列名称校验
$(document).on("focusout","#lbt-leixing-in",function(){
	if($(this).val()==''&&$('#jcxf-txiangmujg').val()!=''){
		$(this).addClass("f-jiaoyan");
		notifyWarning("诊疗项分类不能为空");
		return false;
	}else if($(this).val().length>100&&$('#jcxf-txiangmujg').val()!=''){
  		$(this).addClass("f-jiaoyan");
		notifyWarning("诊疗项分类长度应小于100");
		return false;
	}else{
		$(this).removeClass("f-jiaoyan");
		return false;
	}
});
$(document).on("focusout","#lb-leixing-in",function(){
	if($(this).val()==''&&$('#xiangmujg').val()!=''&&$('#xiangmumc').val()!=''){
		$(this).addClass("f-jiaoyan");
		notifyWarning("诊疗项分类不能为空");
		return false;
	}else if($(this).val().length>100&&$('#xiangmujg').val()!=''&&$('#xiangmumc').val()!=''){
  		$(this).addClass("f-jiaoyan");
		notifyWarning("诊疗项分类长度应小于100");
		return false;
	}else{
		$(this).removeClass("f-jiaoyan");
		return false;
	}
});
// 检查项添加列名称校验
$(document).on("focusout","#jcxf-txiangmumc",function(){
	if($(this).val()==''&&$('#jcxf-txiangmujg').val()!=''){
		$(this).addClass("f-jiaoyan");
		notifyWarning("诊疗项名称不能为空");
		return false;
	}else if($(this).val().length>128){
  		$(this).addClass("f-jiaoyan");
		notifyWarning("诊疗项名称长度应小于128");
		return false;
	}else{
		$(this).removeClass("f-jiaoyan");
		return false;
	}
});
// 检查项添加列价格校验
$(document).on("focusout","#jcxf-txiangmujg",function(){
	if($(this).val().length>10){
		var jg = $(this).val();
		jg = getFloatStr(jg);
		$(this).val(jg);
		$(this).addClass("f-jiaoyan");
		notifyWarning("诊疗项价格长度应小于10");
		return false;
	}else{
		var jg = $(this).val();
		jg = getFloatStr(jg);
		$(this).val(jg);
		$(this).removeClass("f-jiaoyan");
		return false;
	}
});
// 检查项保存列价格校验
$(document).on("focusout",".xiangmujgjy",function(){
	if($(this).val().length>10){
		var jg = $(this).val();
		jg = getFloatStr(jg);
		$(this).val(jg);
		$(this).addClass("f-jiaoyan");
		notifyWarning("诊疗项价格长度应小于10");
		return false;
	}else{
		var jg = $(this).val();
		jg = getFloatStr(jg);
		$(this).val(jg);
		$(this).removeClass("f-jiaoyan");
		return false;
	}
});

// 检查项添加列名称校验
$(document).on('keyup','.xiangmumcjy',function(){
	if($(this).val() == ''){
		$(this).addClass('isClick');
	}
	
})
$(document).on('focusout','.xiangmumcjy',function(){
	if($(this).hasClass('isClick')){
		return false;
	}
	if($(this).val()==''){
		$(this).addClass("f-jiaoyan");
		notifyWarning("诊疗项名称不能为空");
		return false;
	}else if($(this).val().length>128){
  		$(this).addClass("f-jiaoyan");
		notifyWarning("诊疗项名称长度应小于128");
		return false;
	}else{
		$(this).removeClass("f-jiaoyan");
		return false;
	}
});

/** 
 * @Title: getJianchaxcs 
 * @Description: 医保目录跳转获取检查项参数
 * @param preYeMiancs
 */
function getJianchaxcs(preYeMiancs) {
	var YeMiancs = JSON.parse(preYeMiancs);
	if(YeMiancs != null) {
		pageNumber = YeMiancs.pageNo;
		dingweiid = YeMiancs.id;
		dwpanduan = true;
	}
	getJianChax();
}

// 检查项名称检索
var jianchaxFlag;
var Flag;
$(document).on('input, focusin','#jcxf-txiangmumc, .xiangmumcjy', function () {
	var strli = '';
	var jiansuom = $(this).val();
	var obj = this;
	var leixing =  $(this).parent().parent().find('.xiangmufl').val();
	clearTimeout(jianchaxFlag);
	jianchaxFlag = setTimeout(function () {
	if (leixing == '') {
		strli = getZdJianChaxByJsm(jiansuom);
	} else {
		strli = getZdJianChaxByJsmAndLx(jiansuom, leixing);
	}
	$(obj).parent().find('ul').html("");
	$(obj).parent().find('ul').html(strli).css('display', 'block');
	},200);
});


/** 
 * @Title: getZdJianChaxByJsm 
 * @Description: 通过检索码获取字典检查项
 * @param jiansuom
 * @returns {String}
 */
function getZdJianChaxByJsm(jiansuom) {
	var strli = '';
	$.ajax({
		url:'./zidian/getZdJianChaxByJsm',
		type:'post',
		data:{"jiansuom": jiansuom},
		async:false,
		success:function(data) {
			if (data == '') {return false;}
			$.each(data,function(i, v){
				strli += '<li index='+this.leixing+','+this.mingcheng+'>'+this.quancheng+'</li>';
				});
		}
	});
	return strli;
}

/** 
 * @Title: getZdJianChaxByJsmAndLx 
 * @Description:  通过检索码和类型获取字典检查项
 * @param jiansuom
 * @param leixing
 * @returns {String}
 */
function getZdJianChaxByJsmAndLx(jiansuom, leixing) {
	var strli = '';
	var jsonPaams = {
		"jiansuom": jiansuom,
		"leixing": leixing
	};
	$.ajax({
		url: './zidian/getZdJianChaxByJsmAndLx',
		type: 'post',
		data: {"strParams": JSON.stringify(jsonPaams)},
		async: false,
		success: function(data) {
			$.each(data, function (i, v){
				strli += '<li index="'+this.leixing+','+this.mingcheng+'">'+this.mingcheng+'</li>';
			});
		}
	});
	return strli;
}

/** 
 * @Title: addJianChaxlx 
 * @Description: 增加检查项类别LI
 */
function addJianChaxlx() {
	var strli = '';
	$.ajax({
		type: "get",
		url: path + "/zidian/getZdJianChaxlxByJsm",
		async: false,
		success: function (data) {
		$.each(data, function (i, v) {
			strli += '<li value=' + this.leixing+' >' + this.leixing + '</li>';
		});
		}
	});
	$('#lbt-leixing-ul, .jiachaxlb').html(strli);
}

// 检查项类型Li点击
$(document).on('click','#lbt-leixing-ul li, .jiachaxlb li', function () {
	var leixing = $(this).attr('value');
	$(this).parent().parent().find('.xiangmufl').val(leixing);
	$(this).parent().css('display', 'none');
});

// 检查项名称li点击
$(document).on('click','.jianchax li', function () {
	var str = $(this).attr('index').split(',');
	$(this).parent().parent().find('.jianchaxmc').val(str[1]).removeClass('isClick');
	$(this).parent().parent().parent().find('.xiangmufl').val(str[0]);
	$(this).parent().css('display', 'none');
});

//检查项名称检索
var jianchaxFlag;
var Flag;
$(document).on('input','#jcxf-txiangmumc, .xiangmumcjy', function () {
    var strli = '';
    var jiansuom = $(this).val();
    var obj = this;
    var leixing =  $(this).parent().parent().find('.xiangmufl').val();
    clearTimeout(jianchaxFlag);
    jianchaxFlag = setTimeout(function () {
    if (leixing == '') {
        strli = getZdJianChaxByJsm(jiansuom);
    } else {
        strli = getZdJianChaxByJsmAndLx(jiansuom, leixing);
    }
    $(obj).parent().find('ul').html("");
    $(obj).parent().find('ul').append(strli).css('display', 'block');
    },200);
});